import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.*;

public class Login extends JFrame implements ActionListener{
	
	JPanel jp_1, jp_2, jp_3, jp_4 = null;
	JLabel jlb_1, jlb_2, jlb_3 = null;
	JTextField jtf = null;
	JPasswordField jpf = null;
	JRadioButton jrb_1, jrb_2 = null;
	ButtonGroup bg = null;
	JButton jb_1, jb_2, jb_3 = null;
	String user_name;
	String password;
		String stu_name;
	String sno;
	String tea_name;
	String tno;
//数据库
	static String dbURL =  "jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false";
	static String userName="root";
	static String userPwd="0118";

	static Connection ct = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
		
	public static void main(String[] args){

		Login login = new Login();
		try {
			DataSource dataSource = new MysqlDataSource();
			((MysqlDataSource) dataSource).setURL(dbURL);
             ((MysqlDataSource) dataSource).setUser(userName);
			 ((MysqlDataSource) dataSource).setPassword(userPwd);
			 ct = dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public Login()
	{

		jp_1 = new JPanel();
		jp_2 = new JPanel();
		jp_3 = new JPanel();
		jp_4 = new JPanel();



		jlb_1 = new JLabel("用户名");
		jlb_2 = new JLabel("密   码");
		jlb_3 = new JLabel("身   份");


		jtf = new JTextField(10);
		
		jpf = new JPasswordField(10);
		
		jrb_1 = new JRadioButton("教师");
		jrb_2 = new JRadioButton("学生");	


		bg = new ButtonGroup();
		bg.add(jrb_1);
		bg.add(jrb_2);
		jrb_2.setSelected(true);
		
		jb_1 = new JButton("登录");
		jb_2 = new JButton("重置");
		jb_3 = new JButton("退出");	
		jb_1.addActionListener(this);
		jb_2.addActionListener(this);
		jb_3.addActionListener(this);
		jb_1.setBackground(Color.GRAY);
		jb_2.setBackground(Color.GRAY);
		jb_3.setBackground(Color.GRAY);


		jp_1.add(jlb_1);
		jp_1.add(jtf);
		
		jp_2.add(jlb_2);
		jp_2.add(jpf);
		
		jp_3.add(jlb_3);
		jp_3.add(jrb_1);
		jp_3.add(jrb_2);
		
		jp_4.add(jb_1);
		jp_4.add(jb_2);
		jp_4.add(jb_3);
		
		this.add(jp_1);
		this.add(jp_2);
		this.add(jp_3);
		this.add(jp_4);
		
				
		this.setLayout(new GridLayout(4,1));
		
		this.setTitle("河池学院学生成绩管理系统");
		
		this.setSize(500, 300);

		this.setLocationRelativeTo(null);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		URL resource = Login.class.getClassLoader().getResource("河池学院.jfif");
		Image image = new ImageIcon(resource).getImage();
		this.setIconImage(image);

		this.setVisible(true);
		this.setResizable(true);		
	}
	
	

	public void clear(){
		jtf.setText("");
		jpf.setText("");
	}
		

	public void actionPerformed(ActionEvent e){	
		if(e.getActionCommand() == "登录"){
			if(jrb_1.isSelected()){
				try {
					ps = ct.prepareStatement("select * from Teacher where 身份=? and 用户名=?");

					ps.setString(1, "教师");					
					ps.setString(2, jtf.getText());


					rs = ps.executeQuery();
					if(rs.next()){					

						user_name = rs.getString(2);
						password = rs.getString(3);						 
						tea_name = rs.getString(5);						 
						tno = rs.getString(4);
						tea_login();
					}else {
						JOptionPane.showMessageDialog(null, "没有此用户或用户名为空！\n请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
					}
				}catch (SQLException e1) {
					e1.printStackTrace();
				}							
			}else if(jrb_2.isSelected()) {
				try {
					ps = ct.prepareStatement("select * from Student where 身份=? and 用户名=?");

					ps.setString(1, "学生");
					ps.setString(2, jtf.getText());


					rs=ps.executeQuery();
					if(rs.next()){

						user_name = rs.getString(2);
						password = rs.getString(3);					 
						stu_name = rs.getString(5);
						sno = rs.getString(4);						 
						stu_login();
					}else {
						JOptionPane.showMessageDialog(null, "没有此用户或用户名为空！\n请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
					}
				} catch (SQLException e1) {					
					e1.printStackTrace();
				}												
			}
		}else if(e.getActionCommand() == "重置"){	
			clear();
		}else if(e.getActionCommand() == "退出"){		
			System.exit(0);
		}	
	}
	
	

	public void stu_login(){
		if(user_name.equals(jtf.getText()) && password.equals(String.valueOf(jpf.getPassword()))){

			JOptionPane.showMessageDialog(null, "登陆成功！", "提示消息", JOptionPane.WARNING_MESSAGE);		
			clear();		
			dispose();
			Student stu = new Student(stu_name, sno);
		}else if(jtf.getText().isEmpty() && String.valueOf(jpf.getPassword()).isEmpty()){	
			JOptionPane.showMessageDialog(null, "请输入用户名和密码！", "提示消息", JOptionPane.WARNING_MESSAGE);	
		}else if(jtf.getText().isEmpty()){		
			JOptionPane.showMessageDialog(null, "请输入用户名！", "提示信息", JOptionPane.WARNING_MESSAGE);	
		}else if(String.valueOf(jpf.getPassword()).isEmpty()){		
			JOptionPane.showMessageDialog(null, "请输入密码！", "提示信息", JOptionPane.WARNING_MESSAGE);		
		}else{			
			JOptionPane.showMessageDialog(null, "用户名或者密码错误！\n请重新输入", "提示信息", JOptionPane.ERROR_MESSAGE);
			clear();			
		}
	}
	
	

	public void tea_login() {
		if(user_name.equals(jtf.getText()) && password.equals(String.valueOf(jpf.getPassword()))) {		
			JOptionPane.showMessageDialog(null, "登录成功", "提示信息", JOptionPane.WARNING_MESSAGE);		
			clear();		
			dispose();		
			Teacher tea = new Teacher(tea_name, tno);			
		}else if(jtf.getText().isEmpty() && String.valueOf(jpf.getPassword()).isEmpty()) {		
			JOptionPane.showMessageDialog(null, "请输入用户名和密码！", "提示信息", JOptionPane.WARNING_MESSAGE);		
		}else if(jtf.getText().isEmpty()) {	
			JOptionPane.showMessageDialog(null, "请输入用户名！", "提示信息", JOptionPane.WARNING_MESSAGE);	
		}else if(String.valueOf(jpf.getPassword()).isEmpty()) {		
			JOptionPane.showMessageDialog(null, "请输入密码！", "提示信息", JOptionPane.WARNING_MESSAGE);	
		}else {	
			JOptionPane.showMessageDialog(null, "用户名或密码错误！\n请重新输入", "提示信息", JOptionPane.ERROR_MESSAGE);	
			clear();
		}		
	}
}

